package com.czkj.hwz.decorate.Activity;

import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.czkj.hwz.decorate.MyApp;
import com.czkj.hwz.decorate.R;
import com.czkj.hwz.decorate.bean.Enterprise;
import com.czkj.hwz.decorate.fragment.BaseOperationCenterFragment;
import com.czkj.hwz.decorate.fragment.FragmentFactory;
import com.king.base.BaseActivity;
import com.king.base.util.ToastUtils;

import java.util.ArrayList;

public class ProductActivity extends BaseActivity{
    private static final String TAG = "ProductActivity";
    private TabLayout tab;
    private ViewPager viewPager;
    private EditText search_text;
    private ArrayList<Enterprise> mEnterpriseList;
    private String mEnterpriseListStr;
    private int enterpriseid;//运营中心编号
    private String searchText;
    private ProductPagerAdapter productPagerAdapter;
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(getResources().getColor(R.color.yellow));
        }
        setContentView(R.layout.activity_product);
        getSupportActionBar().hide();
        tab=findView(R.id.tab);
        viewPager=findView(R.id.viewpager);
        search_text=findView(R.id.search_text);
        String keyword=getIntent().getStringExtra("keyword");
        search_text.setText(keyword);
        mEnterpriseList= (ArrayList<Enterprise>) getIntent().getSerializableExtra("mEnterpriseList");
        if (mEnterpriseList==null) {
            MyApp myApp= (MyApp) getApplication();
            mEnterpriseList= (ArrayList<Enterprise>) myApp.getmEnterpriseList();
        }
        Log.d(TAG, "initUI: "+mEnterpriseList.toString());
        mEnterpriseListStr=mEnterpriseList.toString().replaceAll("\\[","").replaceAll("\\]","");
        enterpriseid=mEnterpriseList.get(0).getId();
        Log.d(TAG, TAG+enterpriseid);

    }





    @Override
    public void initData() {
        productPagerAdapter=new ProductPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(productPagerAdapter);
        tab.setupWithViewPager(viewPager);
    }

    @Override
    public void addListeners() {
        search_text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_SEARCH) {
                    if (TextUtils.isEmpty(search_text.getText().toString())) {
                        ToastUtils.showToast(context,"请输入关键字");
                    }else {
                        searchText=search_text.getText().toString().trim();
                        viewPager.setCurrentItem(0);
                        productPagerAdapter.notifyDataSetChanged();
                    }
                }
                return false;
            }
        });

    }
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;


        }
    }

    private class ProductPagerAdapter extends FragmentPagerAdapter {
        public String[] mTitle;
        public ProductPagerAdapter(FragmentManager fm) {
            super(fm);
            mTitle=mEnterpriseListStr.split(",");
        }

        @Override
        public Fragment getItem(int position) {
            enterpriseid = mEnterpriseList.get(position).getId();
            BaseOperationCenterFragment baseOperationCenterFragment = FragmentFactory.createFragment(position, enterpriseid,searchText,mTitle[position]);
            return baseOperationCenterFragment;
        }

        @Override
        public int getCount() {
            return mTitle.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            enterpriseid = mEnterpriseList.get(position).getId();
            BaseOperationCenterFragment baseOperationCenterFragment= (BaseOperationCenterFragment) super.instantiateItem(container, position);
            baseOperationCenterFragment.updateArguments(enterpriseid,searchText,mTitle[position]);
            return baseOperationCenterFragment;
        }
    }
}
